package io;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import org.junit.Test;

public class BufferedDemo {
	@Test
	public void write(){
		try (FileOutputStream fos = new FileOutputStream("a.txt");
			 BufferedOutputStream bos = new BufferedOutputStream(fos);){
			// 通过处理流写出去
			bos.write("hello world".getBytes());
			bos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void read() {
		try (FileInputStream fis = new FileInputStream("a.txt");
			BufferedInputStream bis = new BufferedInputStream(fis);){
		    // 通过处理流读入
			byte[] content = new byte[fis.available()];
			bis.read(content);
			
			System.out.println(new String(content));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	@Test
	public void write2(){
		try (FileWriter fos = new FileWriter("a.txt");
			 BufferedWriter bos = new BufferedWriter(fos);){
			// 通过处理流写出去
			bos.write("今天天气不错");
			bos.flush();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	@Test
	public void read2() {
		try (FileReader fis = new FileReader("a.txt");
			BufferedReader bis = new BufferedReader(fis);){
		    // 通过处理流读入
			char[] content = new char[20];
			bis.read(content);
			System.out.println(content);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	
}
